package com.abso.mp3tunes.locker.ui.properties;

import java.util.Iterator;
import java.util.List;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.abso.mp3tunes.locker.core.data.Artist;
import com.abso.mp3tunes.locker.core.data.LockerDataUtils;

/**
 * A specialized section showing the properties of an artist.
 */
public class ArtistPropertySection extends AbstractLockerPropertySection {

	/** The text control used to show the artist's name. */
	private Text nameText;

	/** The text control used to show the number of albums. */
	private Text albumCountText;

	/** The text control used to show the number of tracks. */
	private Text trackCountText;

	/** The text control used to show the total size. */
	private Text sizeText;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite,
	 *      org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	public void createControls(Composite parent,
			TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);
		Composite mainComposite = createMainControl(parent);
		createCLabel(mainComposite, "Name:");
		nameText = createText(mainComposite, false);
		createCLabel(mainComposite, "Nr. of Albums:");
		albumCountText = createText(mainComposite, false);
		createCLabel(mainComposite, "Nr. of Tracks:");
		trackCountText = createText(mainComposite, false);
		createCLabel(mainComposite, "Size:");
		sizeText = createText(mainComposite, false);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#refresh()
	 */
	public void refresh() {
		List elems = getSelectedElements();
		if (elems.size() == 1) {
			Artist artist = (Artist) elems.get(0);
			nameText.setText(artist.getName());
			albumCountText.setText(Integer.toString(artist.getAlbumCount()));
			trackCountText.setText(Integer.toString(artist.getTrackCount()));
			sizeText.setText(LockerDataUtils.getPrettyPrintedSize(artist
					.getSize()));
		} else {
			int albumCount = 0;
			int trackCount = 0;
			long size = 0L;
			for (Iterator i = elems.iterator(); i.hasNext();) {
				Artist artist = (Artist) i.next();
				albumCount += artist.getAlbumCount();
				trackCount += artist.getTrackCount();
				size += artist.getSize();
			}
			nameText.setText(elems.size() + " artists");
			albumCountText.setText(Integer.toString(albumCount));
			trackCountText.setText(Integer.toString(trackCount));
			sizeText.setText(LockerDataUtils.getPrettyPrintedSize(size));
		}
	}

}
